# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity_study_code
File Name: yzm.py
Author: kij
Create Date: 2021/9/2
-------------------------------------------------
"""

import time
import execjs

import os
import re

def get_js():
    # 读文件
    # 1.获取当前文件 读文件操作.py 的路径
    abs = os.path.abspath(__file__)
    # 找到上级目录 dirname
    path = os.path.dirname(abs)
    base_dir = os.path.dirname(os.path.abspath(__file__))
    # 路径拼接 ()可以跟多了路径
    file_path = os.path.join(base_dir, "jiami.js")
    # 2.判断文件是否存在
    if not os.path.exists(file_path):
        print("文件夹不存在就创建")
    # os.makedirs(file_path)
    else:
        # 字节读取
        file_object = open(file_path, mode='rb')
        data = file_object.read()
        file_object.close()
        text = data.decode('utf-8')
        # 文本读取
        file_text = open(file_path, mode='rt', encoding='utf-8')
        file_message = file_text.read()
        file_text.close()
    return file_message


def yzm(fpdm:str,fphm:str,now_time:str):
    # "b85d353e5ffb292bf6df6545821189cd"
    js_message = get_js()
    ctx = execjs.compile(js_message)

    jm_fpdm = ctx.call("encrypt", fpdm)
    jm_fphm = ctx.call("encrypt", fphm)
    jm_now_time = ctx.call("encrypt", now_time)

    _0x3a01ad = [fpdm + fphm,
                 fpdm + fphm + fphm + fpdm,
                 fpdm + fpdm + fphm + fpdm + fphm + fpdm]

    jiami1 = f'{ctx.call("encrypt", "".join(sorted(list(_0x3a01ad[0][:8]))))}{"".join(sorted(list(_0x3a01ad[0][8:])))}'
    jiami2 = f'{"".join(sorted(list(_0x3a01ad[1][:9])))}{ctx.call("encrypt", "".join(sorted(list(_0x3a01ad[1][9:17]))))}{"".join(sorted(list(_0x3a01ad[1][17:])))}'
    jiami3 = f'{"".join(sorted(list(_0x3a01ad[2][:15])))}{"".join(sorted(list(_0x3a01ad[2][15:30])))}{ctx.call("encrypt", "".join(sorted(list(_0x3a01ad[2][30:]))))}'



    j_o = int(fphm) % 2
    new_array = []
    new_array2 = []
    for item in range(len(list(_0x3a01ad[0]))):
        if int(list(_0x3a01ad[0])[item]) % 2 == j_o:
            new_array.append(list(_0x3a01ad[0])[item])
    for item in range(len(list(_0x3a01ad[1]))):
        if int(list(_0x3a01ad[1])[item]) % 2 == j_o:
            new_array2.append(list(_0x3a01ad[1])[item])
    jiami4 = f'123456{"".join(new_array)}'
    jiami5 = f'654321{"".join(new_array2)}'
    new_str = "".join(sorted(list(set(_0x3a01ad[2]))))
    jiami6 = ctx.call("_0x279f8c", new_str)


    jiami8 = f'{jm_now_time}{jm_fphm}{jm_fpdm}{ctx.call("encrypt", jiami1)}{ctx.call("encrypt", jiami2)}{ctx.call("encrypt", jiami3)}{ctx.call("encrypt", jiami4)}{ctx.call("encrypt", jiami5)}{ctx.call("encrypt", jiami6)}{ctx.call("_0x1a0478", jiami1,jiami2,jiami3,jiami4)}'

    jiami9 = f'{jiami8}{ctx.call("_0x1a0478", jiami4, jiami5, fpdm, now_time)}{ctx.call("_0x279f8c", now_time)}'

    jiami10 = ctx.call("encrypt", f'{jiami9}{jm_fpdm}{jm_fphm}{jm_now_time}')
    return jiami10


if __name__ == "__main__":
    print('开始验证码')
    print(time.strftime("%Y-%m-%d %H:%M:%S"))
    # yzm("1200212130", "01674067", '1630566765229')
    # yzm("1200212130", "01674067", '1630566765229')
    yzm("011002100111", "78490309", '1629786620152')
